package com.sz.youban.dt.service.credit;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sz.youban.dt.entity.credit.DtCreditJxl;
import com.sz.youban.dt.entity.result.ResultModelEnum;
import com.sz.youban.dt.interfs.credit.IDtJuXinLiService;
import com.sz.youban.dt.model.MsgException;
import com.sz.youban.dt.service.credit.mapper.DtCreditJxlMapper;
import com.sz.youban.dt.service.utils.GO;
import com.sz.youban.dt.service.utils.HttpUtils;

@Service
public class JuXinLiServiceImpl extends ServiceImpl<DtCreditJxlMapper, DtCreditJxl> implements IDtJuXinLiService
{
	@Value("${develop}")
	private boolean develop;

	private final Logger logger = LoggerFactory.getLogger(JuXinLiServiceImpl.class);

	@Autowired
	private DtCreditJxlMapper creditJxlMapper;

	private static final String testResult = "{\n" +
			"    \"data\": {\n" +
			"        \"auth_org\": \"youbanjinrong\",\n" +
			"        \"update_time\": 1498471857914,\n" +
			"        \"user_basic\": {\n" +
			"            \"user_age\": 42,\n" +
			"            \"user_city\": \"九江市\",\n" +
			"            \"user_gender\": \"男\",\n" +
			"            \"user_idcard\": \"36042819750626601X\",\n" +
			"            \"user_idcard_valid\": true,\n" +
			"            \"user_name\": \"周东华\",\n" +
			"            \"user_phone\": \"18870070777\",\n" +
			"            \"user_phone_city\": \"南昌\",\n" +
			"            \"user_phone_operator\": \"中国移动\",\n" +
			"            \"user_phone_province\": \"江西\",\n" +
			"            \"user_province\": \"江西省\",\n" +
			"            \"user_region\": \"都昌县\"\n" +
			"        },\n" +
			"        \"user_blacklist\": {\n" +
			"            \"blacklist_category\": [\n" +
			"                \"法院\"\n" +
			"            ],\n" +
			"            \"blacklist_details\": [\n" +
			"                {\n" +
			"                    \"details_key\": \"发布日期\",\n" +
			"                    \"details_value\": \"2016年04月25日\"\n" +
			"                },\n" +
			"                {\n" +
			"                    \"details_key\": \"省份\",\n" +
			"                    \"details_value\": \"江西\"\n" +
			"                },\n" +
			"                {\n" +
			"                    \"details_key\": \"性别\",\n" +
			"                    \"details_value\": \"男\"\n" +
			"                },\n" +
			"                {\n" +
			"                    \"details_key\": \"做出执行依据单位\",\n" +
			"                    \"details_value\": \"都昌县人民法院\"\n" +
			"                },\n" +
			"                {\n" +
			"                    \"details_key\": \"案号\",\n" +
			"                    \"details_value\": \"(2016)赣0428执42号\"\n" +
			"                },\n" +
			"                {\n" +
			"                    \"details_key\": \"执行法院\",\n" +
			"                    \"details_value\": \"都昌县人民法院\"\n" +
			"                },\n" +
			"                {\n" +
			"                    \"details_key\": \"失信被执行人行为具体情形\",\n" +
			"                    \"details_value\": \"违反财产报告制度\"\n" +
			"                },\n" +
			"                {\n" +
			"                    \"details_key\": \"立案时间\",\n" +
			"                    \"details_value\": \"2016年01月22日\"\n" +
			"                },\n" +
			"                {\n" +
			"                    \"details_key\": \"执行依据文号\",\n" +
			"                    \"details_value\": \"(2013)都民一初字第1015号民事判决书民事判决书\"\n" +
			"                },\n" +
			"                {\n" +
			"                    \"details_key\": \"年龄\",\n" +
			"                    \"details_value\": \"41\"\n" +
			"                },\n" +
			"                {\n" +
			"                    \"details_key\": \"生效法律文书确定的义务\",\n" +
			"                    \"details_value\": \"限被告赵建波、曹美华于本判决生效后三日内一次性归还本金20000元及截止清偿之日的利息；被告周东华、黄银初、熊龙炳和陈正英对此债务在最高额人民币24000元的范围内承担连带清偿责任。 限被告周东华、袁鲜萍于本判决生效之日内一次性归还原告本金30000元及截止清偿之日的利息，被告赵建波、曹美华、黄银初、熊龙炳和陈正英对此债务在最高额人民币36000元的范围内承担连带清偿责任。 限被告黄银初于本判决生效之日内一次性归还原告本金20000元及截止清偿之日的利息，被告周东华和袁鲜萍、赵建波和曹美华、熊龙炳和陈正英对此债务在最高额人民币24000元的范围内承担连带清偿责任。 限被告熊龙炳、陈正英于本判决生效之日内一次性归还原告本金30000元及截止清偿之日的利息，被告赵建波和曹美华、黄银初、周东华和袁鲜萍对此债务在最高额人民币36000元的范围内承担连带清偿责任。\"\n" +
			"                },\n" +
			"                {\n" +
			"                    \"details_key\": \"被执行人的履行情况\",\n" +
			"                    \"details_value\": \"全部未履行\"\n" +
			"                }\n" +
			"            ],\n" +
			"            \"blacklist_name_with_idcard\": true,\n" +
			"            \"blacklist_name_with_phone\": false,\n" +
			"            \"blacklist_update_time_name_idcard\": \"2016-07-06 10:24:15\",\n" +
			"            \"blacklist_update_time_name_phone\": \"\"\n" +
			"        },\n" +
			"        \"user_gray\": {\n" +
			"            \"contacts_class1_blacklist_cnt\": 2,\n" +
			"            \"contacts_class1_cnt\": 31,\n" +
			"            \"contacts_class2_blacklist_cnt\": 33,\n" +
			"            \"contacts_router_cnt\": 15,\n" +
			"            \"contacts_router_ratio\": 0.4838709677419355,\n" +
			"            \"phone_gray_score\": 0,\n" +
			"            \"user_phone\": \"18870070777\"\n" +
			"        },\n" +
			"        \"user_grid_id\": \"bf794b86-5a57-11e7-bc79-525400c928e6-4313\",\n" +
			"        \"user_idcard_suspicion\": {\n" +
			"            \"idcard_applied_in_orgs\": [\n" +
			"                {\n" +
			"                    \"susp_org_type\": \"其他\",\n" +
			"                    \"susp_updt\": \"2017-06-26 16:36:01\"\n" +
			"                }\n" +
			"            ],\n" +
			"            \"idcard_with_other_names\": [],\n" +
			"            \"idcard_with_other_phones\": []\n" +
			"        },\n" +
			"        \"user_phone_suspicion\": {\n" +
			"            \"phone_applied_in_orgs\": [\n" +
			"                {\n" +
			"                    \"susp_org_type\": \"其他\",\n" +
			"                    \"susp_updt\": \"2017-06-26 16:36:01\"\n" +
			"                }\n" +
			"            ],\n" +
			"            \"phone_with_other_idcards\": [],\n" +
			"            \"phone_with_other_names\": []\n" +
			"        },\n" +
			"        \"user_register_orgs\": {\n" +
			"            \"phone_num\": \"18870070777\",\n" +
			"            \"register_cnt\": null,\n" +
			"            \"register_orgs\": [],\n" +
			"            \"register_orgs_statistics\": []\n" +
			"        },\n" +
			"        \"user_searched_history_by_orgs\": [\n" +
			"            {\n" +
			"                \"org_self\": true,\n" +
			"                \"searched_date\": \"2017-06-26\",\n" +
			"                \"searched_org\": \"其他\"\n" +
			"            }\n" +
			"        ],\n" +
			"        \"user_searched_statistic\": {\n" +
			"            \"searched_org_cnt\": 1\n" +
			"        }\n" +
			"    },\n" +
			"    \"message\": \"获取蜜罐查询成功\",\n" +
			"    \"spend_time\": 180,\n" +
			"    \"code\": \"MIGUAN_SEARCH_SUCCESS\"\n" +
			"}";

	@Override
	@Transactional(rollbackFor = { Exception.class })
	public void saveCreditReport (String name, String idCard, String phone)
	{
		//----------  1，校验该三要素是否已经存在db中，如果存在则拦截----------
		int existResult = 0;
		try {
			existResult = checkExist(name, idCard, phone);
		} catch (Exception e) {
			logger.error("==========ex:{}",e);
		}
		if(existResult > 0){
			//此处是db中已经存在这个人的报告了，则中断向蜜罐发起请求
			return;
		}


		//----------  2, 组织请求参数，存入map----------
		Map<String, String> map = new HashMap<String, String>();
		//聚信立对接的商户信息，后期提到常量文件中
		String url = "https://mi.juxinli.com/api/search";
		map.put("client_secret", "39ce1caef8614c9ca0fda2cf9f2f0413");
		map.put("access_token", "c0e90b2c273c47bf90a49040b88d4990");
		map.put("name", name);
		map.put("id_card", idCard);
		map.put("phone", phone);
		String result = "";

		//----------  3，向蜜罐发起请求，并获取结果----------
		try 
		{
			if(develop){
				result = testResult;
			}else{
				result = HttpUtils.doGet(url, map);
			}

		}
		catch (Exception e) 
		{
			logger.error("ex:{}",e);
			throw new MsgException(ResultModelEnum.REMOTE_INVOKE_FAILED.getCode(),ResultModelEnum.REMOTE_INVOKE_FAILED.getMessage());
		}

		//----------  4，解析result数据并存入db----------
		if(!StringUtils.isEmpty(result))
		{
			//解析存入数据库
			JSONObject jsonObject = JSON.parseObject(result);
			JSONObject data = null;
			if(jsonObject.get("code").equals("MIGUAN_SEARCH_SUCCESS")){
				logger.info("----------蜜罐报告，正常输出结果----------");
				data = (JSONObject) jsonObject.get("data");
				try {
					parseData(data);
					logger.info("---------- 蜜罐报告数据解析、存储成功 ----------");
				} catch (Exception e) {
					logger.error("-------exception:{}",e+"");
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				}

			}else{
				//将蜜罐的异常输出进行返回
				logger.info("----------蜜罐报告，异常输出结果----------{}",jsonObject.get("message"));
				throw new MsgException(ResultModelEnum.REMOTE_INVOKE_FAILED.getCode(), GO.string(jsonObject.get("message")));
			}

		}
		
	}

	private int checkExist(String name, String idCard, String phone) {

		int existResult = creditJxlMapper.checkExist(name, idCard, phone);

		return existResult;
	}

	private void parseData(JSONObject data) throws Exception{

		//----------1，存储 user_basic 及其子信息----------
		DtCreditJxl creditJxlUserBasic = new DtCreditJxl.Builder().parentId(0).rootId(0).key("user_basic").value("基本信息").desc(data.toJSONString()).build();
		{
			creditJxlMapper.insert(creditJxlUserBasic);

			//存储 user_idcard
			{
				String userIdcard = GO.string( ((JSONObject) data.get("user_basic")).get("user_idcard"));
				DtCreditJxl creditJxlUserIdcard = new DtCreditJxl.Builder()
						.parentId(creditJxlUserBasic.getId()).rootId(creditJxlUserBasic.getId()).key("user_idcard").value(userIdcard).desc("身份证号码").build();
				creditJxlMapper.insert(creditJxlUserIdcard);
			}

			//存储 user_phone
			{
				String userPhone = GO.string( ((JSONObject) data.get("user_basic")).get("user_phone"));
				DtCreditJxl creditJxlUserPhone = new DtCreditJxl.Builder()
						.parentId(creditJxlUserBasic.getId()).rootId(creditJxlUserBasic.getId()).key("user_phone").value(userPhone).desc("手机号码").build();
				creditJxlMapper.insert(creditJxlUserPhone);
			}

			//存储 user_idcard_valid
			{
				String userIdcardValid = GO.string( ((JSONObject) data.get("user_basic")).get("user_idcard_valid"));
				DtCreditJxl creditJxlUserIdcardValid = new DtCreditJxl.Builder()
						.parentId(creditJxlUserBasic.getId()).rootId(creditJxlUserBasic.getId()).key("user_idcard_valid").value(userIdcardValid).desc("身份证是否有效").build();
				creditJxlMapper.insert(creditJxlUserIdcardValid);
			}

			//存储 user_phone_province
			{
				String userPhoneProvince = GO.string( ((JSONObject) data.get("user_basic")).get("user_phone_province"));
				DtCreditJxl creditJxlUserPhoneProvince = new DtCreditJxl.Builder()
						.parentId(creditJxlUserBasic.getId()).rootId(creditJxlUserBasic.getId()).key("user_phone_province").value(userPhoneProvince).desc("手机号归属地:省份").build();
				creditJxlMapper.insert(creditJxlUserPhoneProvince);
			}

			//存储 user_city
			{
				String userCity = GO.string( ((JSONObject) data.get("user_basic")).get("user_city"));
				DtCreditJxl creditJxlUserCity = new DtCreditJxl.Builder()
						.parentId(creditJxlUserBasic.getId()).rootId(creditJxlUserBasic.getId()).key("user_city").value(userCity).desc("用户出生地:城市").build();
				creditJxlMapper.insert(creditJxlUserCity);
			}

			//存储 user_phone_city
			{
				String userPhoneCity = GO.string( ((JSONObject) data.get("user_basic")).get("user_phone_city"));
				DtCreditJxl creditJxlUserPhoneCity = new DtCreditJxl.Builder()
						.parentId(creditJxlUserBasic.getId()).rootId(creditJxlUserBasic.getId()).key("user_phone_city").value(userPhoneCity).desc("手机号归属地:城市").build();
				creditJxlMapper.insert(creditJxlUserPhoneCity);
			}

			//存储 user_region
			{
				String userRegion = GO.string( ((JSONObject) data.get("user_basic")).get("user_region"));
				DtCreditJxl creditJxlUserRegion = new DtCreditJxl.Builder()
						.parentId(creditJxlUserBasic.getId()).rootId(creditJxlUserBasic.getId()).key("user_region").value(userRegion).desc("用户出生地:区县").build();
				creditJxlMapper.insert(creditJxlUserRegion);
			}

			//存储 user_phone_operator
			{
				String userPhoneOperator = GO.string( ((JSONObject) data.get("user_basic")).get("user_phone_operator"));
				DtCreditJxl creditJxlUserPhoneOperator = new DtCreditJxl.Builder()
						.parentId(creditJxlUserBasic.getId()).rootId(creditJxlUserBasic.getId()).key("user_phone_operator").value(userPhoneOperator).desc("运营商类别").build();
				creditJxlMapper.insert(creditJxlUserPhoneOperator);
			}

			//存储 user_age
			{
				String userAge = GO.string( ((JSONObject) data.get("user_basic")).get("user_age"));
				DtCreditJxl creditJxlUserAge = new DtCreditJxl.Builder()
						.parentId(creditJxlUserBasic.getId()).rootId(creditJxlUserBasic.getId()).key("user_age").value(userAge).desc("年龄").build();
				creditJxlMapper.insert(creditJxlUserAge);
			}

			//存储 user_gender
			{
				String userGender = GO.string( ((JSONObject) data.get("user_basic")).get("user_gender"));
				DtCreditJxl creditJxlUserGender = new DtCreditJxl.Builder()
						.parentId(creditJxlUserBasic.getId()).rootId(creditJxlUserBasic.getId()).key("user_gender").value(userGender).desc("性别").build();
				creditJxlMapper.insert(creditJxlUserGender);
			}

			//存储 user_province
			{
				String userProvince = GO.string( ((JSONObject) data.get("user_basic")).get("user_province"));
				DtCreditJxl creditJxlUserProvince = new DtCreditJxl.Builder()
						.parentId(creditJxlUserBasic.getId()).rootId(creditJxlUserBasic.getId()).key("user_province").value(userProvince).desc("用户出生地:省份").build();
				creditJxlMapper.insert(creditJxlUserProvince);
			}

			//存储 user_name
			{
				String userName = GO.string( ((JSONObject) data.get("user_basic")).get("user_name"));
				DtCreditJxl creditJxlUserName = new DtCreditJxl.Builder()
						.parentId(creditJxlUserBasic.getId()).rootId(creditJxlUserBasic.getId()).key("user_name").value(userName).desc("姓名").build();
				creditJxlMapper.insert(creditJxlUserName);
			}

		}


		//----------2，存储 user_gray 及其子信息----------
		{
			JSONObject userGray = (JSONObject) data.get("user_gray");
			//存储 user_gray
			DtCreditJxl creditJxlUserGray = new DtCreditJxl.Builder()
					.parentId(0).rootId(creditJxlUserBasic.getId()).key("user_gray").value("用户灰度分数信息").desc("用户灰度分数信息").build();
			logger.info("----------将要存储的creditJxlUserGray对象的内容为：{}",creditJxlUserGray.toString());
			creditJxlMapper.insert(creditJxlUserGray);

			//存储 contacts_class1_blacklist_cnt
			{
				String contactsClass1BlacklistCnt = GO.string(userGray.get("contacts_class1_blacklist_cnt"));
				DtCreditJxl creditJxlContactsClass1BlacklistCnt = new DtCreditJxl.Builder()
						.parentId(creditJxlUserGray.getId()).rootId(creditJxlUserBasic.getId()).key("contacts_class1_blacklist_cnt").value(contactsClass1BlacklistCnt).desc("直接联系人在黑名单的数量").build();
				creditJxlMapper.insert(creditJxlContactsClass1BlacklistCnt);
			}

			//存储 user_phone
			{
				String userPhone = GO.string( ((JSONObject) data.get("user_gray")).get("user_phone"));
				DtCreditJxl creditJxlUserPhone = new DtCreditJxl.Builder()
						.parentId(creditJxlUserGray.getId()).rootId(creditJxlUserBasic.getId()).key("user_phone").value(userPhone).desc("灰度手机号码").build();
				creditJxlMapper.insert(creditJxlUserPhone);
			}

			//存储 contacts_router_ratio
			{
				String contactsRouterRatio = GO.string( ((JSONObject) data.get("user_gray")).get("contacts_router_ratio"));
				DtCreditJxl creditJxlContactsRouterRatio = new DtCreditJxl.Builder()
						.parentId(creditJxlUserGray.getId()).rootId(creditJxlUserBasic.getId()).key("contacts_router_ratio").value(contactsRouterRatio).desc("引起占比=引起二阶黑名单人数/一阶联系人总数").build();
				creditJxlMapper.insert(creditJxlContactsRouterRatio);
			}

			//存储 contacts_class2_blacklist_cnt
			{
				String contactsClass2BlacklistCnt = GO.string( ((JSONObject) data.get("user_gray")).get("contacts_class2_blacklist_cnt"));
				DtCreditJxl creditJxlContactsClass2BlacklistCnt = new DtCreditJxl.Builder()
						.parentId(creditJxlUserGray.getId()).rootId(creditJxlUserBasic.getId()).key("contacts_class2_blacklist_cnt").value(contactsClass2BlacklistCnt).desc("间接联系人在黑名单的数量").build();
				creditJxlMapper.insert(creditJxlContactsClass2BlacklistCnt);
			}

			//存储 contacts_router_cnt
			{
				String contactsRouterCnt = GO.string( ((JSONObject) data.get("user_gray")).get("contacts_router_cnt"));
				DtCreditJxl creditJxlContactsRouterCnt = new DtCreditJxl.Builder()
						.parentId(creditJxlUserGray.getId()).rootId(creditJxlUserBasic.getId()).key("contacts_router_cnt").value(contactsRouterCnt).desc("引起二阶黑名单人数").build();
				creditJxlMapper.insert(creditJxlContactsRouterCnt);
			}

			//存储 contacts_class1_cnt
			{
				String contactsClass1Cnt = GO.string( ((JSONObject) data.get("user_gray")).get("contacts_class1_cnt"));
				DtCreditJxl creditJxlContactsClass1Cnt = new DtCreditJxl.Builder()
						.parentId(creditJxlUserGray.getId()).rootId(creditJxlUserBasic.getId()).key("contacts_class1_cnt").value(contactsClass1Cnt).desc("一阶联系人总数").build();
				creditJxlMapper.insert(creditJxlContactsClass1Cnt);
			}

			//存储 phone_gray_score
			{
				String phoneGrayScore = GO.string( ((JSONObject) data.get("user_gray")).get("phone_gray_score"));
				DtCreditJxl creditJxlPhoneGrayScore = new DtCreditJxl.Builder()
						.parentId(creditJxlUserGray.getId()).rootId(creditJxlUserBasic.getId()).key("phone_gray_score").value(phoneGrayScore).desc("手机号码灰度分数").build();
				creditJxlMapper.insert(creditJxlPhoneGrayScore);
			}

		}

		//----------3，存储  及其子信息----------
		{
			//存储 user_searched_statistic
			DtCreditJxl creditJxlUserSearchedStatistic = new DtCreditJxl.Builder()
					.parentId(0).rootId(creditJxlUserBasic.getId()).key("user_searched_statistic").value("被机构查询数量统计(去重后)").desc("被机构查询数量统计(去重后)").build();
			creditJxlMapper.insert(creditJxlUserSearchedStatistic);

			//存储 searched_org_cnt
			{
				String searchedOrgCnt = GO.string( ((JSONObject) data.get("user_searched_statistic")).get("searched_org_cnt"));
				DtCreditJxl creditJxlSearchedOrgCnt = new DtCreditJxl.Builder()
						.parentId(creditJxlUserSearchedStatistic.getId()).rootId(creditJxlUserBasic.getId()).key("searched_org_cnt").value(searchedOrgCnt).desc("被机构查询数量").build();
				creditJxlMapper.insert(creditJxlSearchedOrgCnt);
			}
		}

		//----------4，存储 user_register_orgs 及其子信息----------
		{
			//存储 user_register_orgs
			DtCreditJxl creditJxlUserRegisterOrgs = new DtCreditJxl.Builder()
					.parentId(0).rootId(creditJxlUserBasic.getId()).key("user_register_orgs").value("用户注册信息情况").desc("用户注册信息情况").build();
			creditJxlMapper.insert(creditJxlUserRegisterOrgs);

			//存储 register_cnt
			{
				String registerCnt = GO.string( ((JSONObject) data.get("user_register_orgs")).get("register_cnt"));
				DtCreditJxl creditJxlRegisterCnt = new DtCreditJxl.Builder()
						.parentId(creditJxlUserRegisterOrgs.getId()).rootId(creditJxlUserBasic.getId()).key("register_cnt").value(registerCnt).desc("注册总数量").build();
				creditJxlMapper.insert(creditJxlRegisterCnt);
			}

			//存储 phone_num
			{
				String phoneNum = GO.string( ((JSONObject) data.get("user_register_orgs")).get("phone_num"));
				DtCreditJxl creditJxlPhoneNum = new DtCreditJxl.Builder()
						.parentId(creditJxlUserRegisterOrgs.getId()).rootId(creditJxlUserBasic.getId()).key("phone_num").value(phoneNum).desc("查询手机号码").build();
				creditJxlMapper.insert(creditJxlPhoneNum);
			}

			//存储 user_register_orgs
			{
				String registerOrgs = GO.string(((JSONObject) data.get("user_register_orgs")).get("register_orgs"));
				DtCreditJxl creditJxlRegisterOrgs = new DtCreditJxl.Builder()
						.parentId(creditJxlUserRegisterOrgs.getId()).rootId(creditJxlUserBasic.getId()).key("register_orgs").value(registerOrgs).desc("注册机构").build();
				creditJxlMapper.insert(creditJxlRegisterOrgs);
			}

			//存储 register_orgs_statistics 及其子信息
			{
				DtCreditJxl creditJxlRegisterOrgsStatistics = new DtCreditJxl.Builder()
						.parentId(creditJxlUserRegisterOrgs.getId()).rootId(creditJxlUserBasic.getId()).key("register_orgs_statistics").value("用户注册App统计详情").desc("用户注册App统计详情").build();
				creditJxlMapper.insert(creditJxlRegisterOrgsStatistics);

				//遍历 register_orgs_satistics 内部的数据
				JSONArray registerOrgsStatistics = (JSONArray) ((JSONObject) data.get("user_register_orgs")).get("register_orgs_statistics");
				logger.info("----------registerOrgsStatistics.size:{}",registerOrgsStatistics.size());
				if(registerOrgsStatistics.size()>0){
					for(int i=0;i<registerOrgsStatistics.size();i++){
						JSONObject job = registerOrgsStatistics.getJSONObject(i); // 遍历 jsonarray 数组，把每一个对象转成 json 对象
						//存储 count
						{
							String count = GO.string( job.get("count"));
							DtCreditJxl creditJxlCount = new DtCreditJxl.Builder()
									.parentId(creditJxlRegisterOrgsStatistics.getId()).rootId(creditJxlUserBasic.getId()).key("count").value(count).desc("每种类型注册的数量").build();
							creditJxlMapper.insert(creditJxlCount);
						}

						//存储 label
						{
							String label = GO.string( job.get("label"));
							DtCreditJxl creditJxlLabel = new DtCreditJxl.Builder()
									.parentId(creditJxlRegisterOrgsStatistics.getId()).rootId(creditJxlUserBasic.getId()).key("label").value(label).desc("注册App的类型").build();
							creditJxlMapper.insert(creditJxlLabel);
						}
					}
				}
			}
		}

		//----------5，存储 user_blacklist 及其子信息----------
		{
			DtCreditJxl creditJxlUserBlacklist = new DtCreditJxl.Builder()
					.parentId(0).rootId(creditJxlUserBasic.getId()).key("user_blacklist").value("黑名单信息").desc("黑名单信息").build();
			creditJxlMapper.insert(creditJxlUserBlacklist);

			//存储 blacklist_name_with_phone
			{
				String blacklistNameWithPhone = GO.string( ((JSONObject) data.get("user_blacklist")).get("blacklist_name_with_phone"));
				DtCreditJxl creditJxlBlacklistNameWithPhone = new DtCreditJxl.Builder()
						.parentId(creditJxlUserBlacklist.getId()).rootId(creditJxlUserBasic.getId()).key("blacklist_name_with_phone").value(blacklistNameWithPhone).desc("姓名和手机是否在黑名单").build();
				creditJxlMapper.insert(creditJxlBlacklistNameWithPhone);
			}

			//存储 blacklist_name_with_idcard
			{
				String blacklistNameWithIdcard = GO.string( ((JSONObject) data.get("user_blacklist")).get("blacklist_name_with_idcard"));
				DtCreditJxl creditJxlBlacklistNameWithIdcard = new DtCreditJxl.Builder()
						.parentId(creditJxlUserBlacklist.getId()).rootId(creditJxlUserBasic.getId()).key("blacklist_name_with_idcard").value(blacklistNameWithIdcard).desc("身份证和姓名是否在黑名单").build();
				creditJxlMapper.insert(creditJxlBlacklistNameWithIdcard);
			}

			//存储 blacklist_category
			{
				String blacklistCategory = GO.string( ((JSONObject) data.get("user_blacklist")).get("blacklist_category"));
				DtCreditJxl creditJxlBlacklistCategory = new DtCreditJxl.Builder()
						.parentId(creditJxlUserBlacklist.getId()).rootId(creditJxlUserBasic.getId()).key("blacklist_category").value(blacklistCategory).desc("黑名单分类").build();
				creditJxlMapper.insert(creditJxlBlacklistCategory);
			}

			//存储 blacklist_details 及其子信息
			{
//				DtCreditJxl creditJxlBlacklistDetails = new DtCreditJxl(creditJxlUserBlacklist.getId(), creditJxlUserBasic.getId(), "blacklist_details", "黑名单详情", null, "黑名单详情");
				DtCreditJxl creditJxlBlacklistDetails = new DtCreditJxl.Builder()
						.parentId(creditJxlUserBlacklist.getId()).rootId(creditJxlUserBasic.getId()).key("blacklist_details").value("黑名单详情").desc("黑名单详情").build();
				creditJxlMapper.insert(creditJxlBlacklistDetails);

				//遍历 blacklist_details 内部的数据
				JSONArray blacklistDetails = (JSONArray) ((JSONObject) data.get("user_blacklist")).get("blacklist_details");
				if(blacklistDetails.size()>0){
					for(int i=0;i<blacklistDetails.size();i++){
						JSONObject job = blacklistDetails.getJSONObject(i); // 遍历 jsonarray 数组，把每一个对象转成 json 对象
						//存储 details_key
						{
							String detailsKey = GO.string( job.get("details_key"));
							DtCreditJxl creditJxlDetailsKey = new DtCreditJxl.Builder()
									.parentId(creditJxlBlacklistDetails.getId()).rootId(creditJxlUserBasic.getId()).key("details_key").value(detailsKey).desc("详情名称").build();
							creditJxlMapper.insert(creditJxlDetailsKey);
						}

						//存储 details_value
						{
							String detailsValue = GO.string( job.get("details_value"));
							DtCreditJxl creditJxlDetailsValue = new DtCreditJxl.Builder()
									.parentId(creditJxlBlacklistDetails.getId()).rootId(creditJxlUserBasic.getId()).key("details_value").bigValue(detailsValue).desc("详情内容").build();
							creditJxlMapper.insert(creditJxlDetailsValue);
						}
					}
				}
			}
		}

		//----------6，存储 user_phone_suspicion 及其子信息----------
		{
			DtCreditJxl creditJxlUserPhoneSuspicion = new DtCreditJxl.Builder()
					.parentId(0).rootId(creditJxlUserBasic.getId()).key("user_phone_suspicion").value("手机号码存疑").desc("手机号码存疑").build();
			creditJxlMapper.insert(creditJxlUserPhoneSuspicion);

			//存储 phone_with_other_idcards 及其子信息
			{
				DtCreditJxl creditJxlPhoneWithOtherIdcards = new DtCreditJxl.Builder()
						.parentId(creditJxlUserPhoneSuspicion.getId()).rootId(creditJxlUserBasic.getId()).key("idcard_with_other_names").value("用这个手机号码绑定的其他身份证").desc("用这个手机号码绑定的其他身份证").build();
				creditJxlMapper.insert(creditJxlPhoneWithOtherIdcards);

				//遍历 phone_with_other_idcards 内部的数据
				JSONArray phoneWithOtherIdcards = (JSONArray) ((JSONObject) data.get("user_phone_suspicion")).get("phone_with_other_idcards");
				if(phoneWithOtherIdcards.size()>0){
					for(int i=0;i<phoneWithOtherIdcards.size();i++){
						JSONObject job = phoneWithOtherIdcards.getJSONObject(i); // 遍历 jsonarray 数组，把每一个对象转成 json 对象
						//存储 susp_idcard
						{
							String suspIdcard = GO.string( job.get("susp_idcard"));
							DtCreditJxl creditJxlSuspIdcard = new DtCreditJxl.Builder()
									.parentId(creditJxlPhoneWithOtherIdcards.getId()).rootId(creditJxlUserBasic.getId()).key("susp_idcard").value(suspIdcard).desc("绑定的身份证").build();
							creditJxlMapper.insert(creditJxlSuspIdcard);
						}

						//存储 susp_updt
						{
							String suspUpdt = GO.string( job.get("susp_updt"));
							DtCreditJxl creditJxlSuspUpdt = new DtCreditJxl.Builder()
									.parentId(creditJxlPhoneWithOtherIdcards.getId()).rootId(creditJxlUserBasic.getId()).key("susp_updt").value(suspUpdt).desc("绑定时间").build();
							creditJxlMapper.insert(creditJxlSuspUpdt);
						}
					}
				}
			}

			//存储 phone_with_other_names 及其子信息
			{
				DtCreditJxl creditJxlPhoneWithOtherINames = new DtCreditJxl.Builder()
						.parentId(creditJxlUserPhoneSuspicion.getId()).rootId(creditJxlUserBasic.getId()).key("phone_with_other_names").value("用这个手机号码绑定的其他姓名").desc("用这个手机号码绑定的其他姓名").build();
				creditJxlMapper.insert(creditJxlPhoneWithOtherINames);

				//遍历 phone_with_other_names 内部的数据
				JSONArray phoneWithOtherNames = (JSONArray) ((JSONObject) data.get("user_phone_suspicion")).get("phone_with_other_names");
				if(phoneWithOtherNames.size()>0){
					for(int i=0;i<phoneWithOtherNames.size();i++){
						JSONObject job = phoneWithOtherNames.getJSONObject(i); // 遍历 jsonarray 数组，把每一个对象转成 json 对象
						//存储 susp_name
						{
							String suspName = GO.string( job.get("susp_name"));
							DtCreditJxl creditJxlSuspName = new DtCreditJxl.Builder()
									.parentId(creditJxlPhoneWithOtherINames.getId()).rootId(creditJxlUserBasic.getId()).key("susp_name").value(suspName).desc("绑定姓名").build();
							creditJxlMapper.insert(creditJxlSuspName);
						}

						//存储 susp_updt
						{
							String suspUpdt = GO.string( job.get("susp_updt"));
							DtCreditJxl creditJxlSuspUpdt = new DtCreditJxl.Builder()
									.parentId(creditJxlPhoneWithOtherINames.getId()).rootId(creditJxlUserBasic.getId()).key("susp_updt").value(suspUpdt).desc("绑定时间").build();
							creditJxlMapper.insert(creditJxlSuspUpdt);
						}
					}
				}
			}

			//存储 phone_applied_in_orgs 及其子信息
			{
				DtCreditJxl creditJxlPhoneAppliedInOrgs = new DtCreditJxl.Builder()
						.parentId(creditJxlUserPhoneSuspicion.getId()).rootId(creditJxlUserBasic.getId()).key("phone_applied_in_orgs").value("电话号码在那些类型的机构中使用过").desc("电话号码在那些类型的机构中使用过").build();
				creditJxlMapper.insert(creditJxlPhoneAppliedInOrgs);

				//遍历 phone_applied_in_orgs 内部的数据
				JSONArray phoneAppliedInOrgs = (JSONArray) ((JSONObject) data.get("user_phone_suspicion")).get("phone_applied_in_orgs");
				if(phoneAppliedInOrgs.size()>0){
					for(int i=0;i<phoneAppliedInOrgs.size();i++){
						JSONObject job = phoneAppliedInOrgs.getJSONObject(i); // 遍历 jsonarray 数组，把每一个对象转成 json 对象
						//存储 susp_org_type
						{
							String suspOrgType = GO.string( job.get("susp_org_type"));
							DtCreditJxl creditJxlSuspOrgType = new DtCreditJxl.Builder()
									.parentId(creditJxlPhoneAppliedInOrgs.getId()).rootId(creditJxlUserBasic.getId()).key("susp_org_type").value(suspOrgType).desc("机构所属分类").build();
							creditJxlMapper.insert(creditJxlSuspOrgType);
						}

						//存储 susp_updt
						{
							String suspUpdt = GO.string( job.get("susp_updt"));
							DtCreditJxl creditJxlSuspUpdt = new DtCreditJxl.Builder()
									.parentId(creditJxlPhoneAppliedInOrgs.getId()).rootId(creditJxlUserBasic.getId()).key("susp_updt").value(suspUpdt).desc("绑定时间").build();
							creditJxlMapper.insert(creditJxlSuspUpdt);
						}
					}
				}
			}
		}

		//----------7，存储 user_idcard_suspicion 及其子信息----------
		{
			DtCreditJxl creditJxlUserIdcardSuspicion = new DtCreditJxl.Builder()
					.parentId(0).rootId(creditJxlUserBasic.getId()).key("user_idcard_suspicion").value("身份证号码存疑").desc("身份证号码存疑").build();
			creditJxlMapper.insert(creditJxlUserIdcardSuspicion);

			//存储 idcard_with_other_names 及其子信息
			{
				DtCreditJxl creditJxlIdcardWithOtherNames = new DtCreditJxl.Builder()
						.parentId(creditJxlUserIdcardSuspicion.getId()).rootId(creditJxlUserBasic.getId()).key("idcard_with_other_names").value("用这个身份证号码绑定的其他姓名").desc("用这个身份证号码绑定的其他姓名").build();
				creditJxlMapper.insert(creditJxlIdcardWithOtherNames);

				//遍历 idcard_with_other_names 内部的数据
				JSONArray idcardWithOtherNames = (JSONArray) ((JSONObject) data.get("user_idcard_suspicion")).get("idcard_with_other_names");
				if(idcardWithOtherNames.size()>0){
					for(int i=0;i<idcardWithOtherNames.size();i++){
						JSONObject job = idcardWithOtherNames.getJSONObject(i); // 遍历 jsonarray 数组，把每一个对象转成 json 对象
						//存储 susp_name
						{
							String suspName = GO.string( job.get("susp_name"));
							DtCreditJxl creditJxlSuspName = new DtCreditJxl.Builder()
									.parentId(creditJxlIdcardWithOtherNames.getId()).rootId(creditJxlUserBasic.getId()).key("susp_name").value(suspName).desc("绑定姓名").build();
							creditJxlMapper.insert(creditJxlSuspName);
						}

						//存储 susp_updt
						{
							String suspUpdt = GO.string( job.get("susp_updt"));
							DtCreditJxl creditJxlSuspUpdt = new DtCreditJxl.Builder()
									.parentId(creditJxlIdcardWithOtherNames.getId()).rootId(creditJxlUserBasic.getId()).key("susp_updt").value(suspUpdt).desc("绑定时间").build();
							creditJxlMapper.insert(creditJxlSuspUpdt);
						}
					}
				}
			}

			//存储 idcard_with_other_phones 及其子信息
			{
				DtCreditJxl creditJxlIdcardWithOtherPhones = new DtCreditJxl.Builder()
						.parentId(creditJxlUserIdcardSuspicion.getId()).rootId(creditJxlUserBasic.getId()).key("idcard_with_other_phones").value("用这个身份证绑定的其他手机号码").desc("用这个身份证绑定的其他手机号码").build();
				creditJxlMapper.insert(creditJxlIdcardWithOtherPhones);

				//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=  coding =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
				//遍历 idcard_with_other_phones 内部的数据
				JSONArray idcardWithOtherPhones = (JSONArray) ((JSONObject) data.get("user_idcard_suspicion")).get("idcard_with_other_phones");
				//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=  coding =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
				if(idcardWithOtherPhones.size()>0){
					for(int i=0;i<idcardWithOtherPhones.size();i++){
						JSONObject job = idcardWithOtherPhones.getJSONObject(i); // 遍历 jsonarray 数组，把每一个对象转成 json 对象
						//存储 susp_phone_province
						{
							String suspPhoneProvince = GO.string( job.get("susp_phone_province"));
							DtCreditJxl creditJxlSuspPhoneProvince = new DtCreditJxl.Builder()
									.parentId(creditJxlIdcardWithOtherPhones.getId()).rootId(creditJxlUserBasic.getId()).key("susp_phone_province").value(suspPhoneProvince).desc("该号码所属省份").build();
							creditJxlMapper.insert(creditJxlSuspPhoneProvince);
						}

						//存储 susp_phone_operator
						{
							String suspPhoneOperator = GO.string( job.get("susp_phone_operator"));
							DtCreditJxl creditJxlSuspPhoneOperator = new DtCreditJxl.Builder()
									.parentId(creditJxlIdcardWithOtherPhones.getId()).rootId(creditJxlUserBasic.getId()).key("susp_phone_operator").value(suspPhoneOperator).desc("该号码所属运营商").build();
							creditJxlMapper.insert(creditJxlSuspPhoneOperator);
						}

						//存储 susp_updt
						{
							String suspUpdt = GO.string( job.get("susp_updt"));
							DtCreditJxl creditJxlSuspUpdt = new DtCreditJxl.Builder()
									.parentId(creditJxlIdcardWithOtherPhones.getId()).rootId(creditJxlUserBasic.getId()).key("susp_updt").value(suspUpdt).desc("该号码最后一次绑定时间").build();
							creditJxlMapper.insert(creditJxlSuspUpdt);
						}

						//存储 susp_phone
						{
							String suspPhone = GO.string( job.get("susp_phone"));
							DtCreditJxl creditJxlSuspPhone = new DtCreditJxl.Builder()
									.parentId(creditJxlIdcardWithOtherPhones.getId()).rootId(creditJxlUserBasic.getId()).key("susp_phone").value(suspPhone).desc("\"存疑手机号码（可能有多个号码）").build();
							creditJxlMapper.insert(creditJxlSuspPhone);
						}

						//存储 susp_phone_city
						{
							String suspPhoneCity = GO.string( job.get("susp_phone_city"));
							DtCreditJxl creditJxlSuspPhoneCity = new DtCreditJxl.Builder()
									.parentId(creditJxlIdcardWithOtherPhones.getId()).rootId(creditJxlUserBasic.getId()).key("susp_phone_city").value(suspPhoneCity).desc("该号码所在地区").build();
							creditJxlMapper.insert(creditJxlSuspPhoneCity);
						}

					}
				}
			}

			//存储 idcard_applied_in_orgs 及其子信息
			{
				DtCreditJxl creditJxlIdcardAppliedOnOrgs = new DtCreditJxl.Builder()
						.parentId(creditJxlUserIdcardSuspicion.getId()).rootId(creditJxlUserBasic.getId()).key("idcard_applied_in_orgs").value("身份证在那些类型的机构中使用过").desc("身份证在那些类型的机构中使用过").build();
				creditJxlMapper.insert(creditJxlIdcardAppliedOnOrgs);

				//遍历 idcard_applied_in_orgs 内部的数据
				JSONArray idcardAppliedInOrgs = (JSONArray) ((JSONObject) data.get("user_idcard_suspicion")).get("idcard_applied_in_orgs");
				if(idcardAppliedInOrgs.size()>0){
					for(int i=0;i<idcardAppliedInOrgs.size();i++){
						JSONObject job = idcardAppliedInOrgs.getJSONObject(i); // 遍历 jsonarray 数组，把每一个对象转成 json 对象
						//存储 susp_org_type
						{
							String suspOrgType = GO.string( job.get("susp_org_type"));
							DtCreditJxl creditJxlSuspOrgType = new DtCreditJxl.Builder()
									.parentId(creditJxlIdcardAppliedOnOrgs.getId()).rootId(creditJxlUserBasic.getId()).key("susp_org_type").value(suspOrgType).desc("机构所属分类").build();
							creditJxlMapper.insert(creditJxlSuspOrgType);
						}

						//存储 susp_updt
						{
							String suspUpdt = GO.string( job.get("susp_updt"));
							DtCreditJxl creditJxlSuspUpdt = new DtCreditJxl.Builder()
									.parentId(creditJxlIdcardAppliedOnOrgs.getId()).rootId(creditJxlUserBasic.getId()).key("susp_updt").value(suspUpdt).desc("查询时间").build();
							creditJxlMapper.insert(creditJxlSuspUpdt);
						}
					}
				}
			}
		}

		//----------8，存储 user_searched_history_by_orgs 及其子信息----------\
		{
			DtCreditJxl creditJxlUserSearchedHistoryByOrgs = new DtCreditJxl.Builder()
					.parentId(0).rootId(creditJxlUserBasic.getId()).key("user_searched_history_by_orgs").value("用户被机构查询历史").desc("用户被机构查询历史").build();
			creditJxlMapper.insert(creditJxlUserSearchedHistoryByOrgs);

			//遍历 user_searched_history_by_orgs 内部的数据
			JSONArray userSearchedHistoryByOrgs = (JSONArray) ((JSONObject) data.get("user_idcard_suspicion")).get("idcard_applied_in_orgs");
			//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=  coding =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
			if(userSearchedHistoryByOrgs.size()>0){
				for(int i=0;i<userSearchedHistoryByOrgs.size();i++){
					JSONObject job = userSearchedHistoryByOrgs.getJSONObject(i); // 遍历 jsonarray 数组，把每一个对象转成 json 对象
					//存储 searched_org
					{
						String searchedOrg = GO.string( job.get("searched_org"));
						DtCreditJxl creditJxlSearchedOrg = new DtCreditJxl.Builder()
								.parentId(creditJxlUserSearchedHistoryByOrgs.getId()).rootId(creditJxlUserBasic.getId()).key("searched_org").value(searchedOrg).desc("主动查询用户信息的机构类型").build();
						creditJxlMapper.insert(creditJxlSearchedOrg);
					}

					//存储 searched_date
					{
						String searchedDate = GO.string( job.get("searched_date"));
						DtCreditJxl creditJxlSearchedDate = new DtCreditJxl.Builder()
								.parentId(creditJxlUserSearchedHistoryByOrgs.getId()).rootId(creditJxlUserBasic.getId()).key("searched_date").value(searchedDate).desc("查询时间").build();
						creditJxlMapper.insert(creditJxlSearchedDate);
					}

					//存储 org_self
					{
						String orgSelf = GO.string( job.get("org_self"));
						DtCreditJxl creditJxlOrgSelf = new DtCreditJxl.Builder()
								.parentId(creditJxlUserSearchedHistoryByOrgs.getId()).rootId(creditJxlUserBasic.getId()).key("org_self").value(orgSelf).desc("是否是本机构查询").build();
						creditJxlMapper.insert(creditJxlOrgSelf);
					}
				}
			}

		}

	}

	public static void main(String arg[]){

		//----------1，获取data----------
		JSONObject jsonObject = JSON.parseObject(testResult);
		JSONObject data = (JSONObject) jsonObject.get("data");
		//----------1，获取data----------
//		String user_phone = (String) ((JSONObject) data.get("user_gray")).get("user_phone");
//		DtCreditJxl creditJxl = new DtCreditJxl(0,0,"user_phone",user_phone,null,"灰度手机号码");


		//遍历 blacklist_details 内部的数据
		JSONArray blacklistDetails = (JSONArray) ((JSONObject) data.get("user_blacklist")).get("blacklist_details");
		if(blacklistDetails.size()>0){
			for(int i=0;i<blacklistDetails.size();i++){
				JSONObject job = blacklistDetails.getJSONObject(i); // 遍历 jsonarray 数组，把每一个对象转成 json 对象
				//存储 details_key
				{
					String detailsKey = (String) job.get("details_key");
					System.out.println("---------- detailsKey的值为：" + detailsKey);
				}

				//存储 details_value
				{
					String detailsValue = (String) job.get("details_value");
					System.out.println("********** detailsValue的值为：" + detailsValue);
				}
			}
		}

	}

	
}
